package com.kykj.kykj.controller;

import com.kykj.kykj.entity.ResultEntity;
import com.kykj.kykj.entity.RoleEntity;
import com.kykj.kykj.service.impl.RoleServiceImpl;
import com.kykj.kykj.vo.RoleVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("v2/role/")
@CrossOrigin//解决跨域问题
@RestController
public class RoleController {
    @Resource
    RoleServiceImpl roleService;

    /**
     * 根据roleid获取对象
     * @param roleid roleid
     * @return ResultEntity
     */
    @GetMapping("role")
    private ResultEntity getRoleById(@RequestParam("roleid") int roleid){
        ResultEntity resultEntity = new ResultEntity();
        try{
            RoleEntity roleEntity = roleService.getRoleById(roleid);
            if(roleEntity!=null){
                resultEntity.setData(roleEntity);
                resultEntity.setCode(0);
                resultEntity.setMsg("数据获取成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("数据获取失败！");
            }
        }catch (Exception exception){
            exception.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("数据获取失败");
        }
        return resultEntity;
    }

    /**
     * 分页获取 role
     * @param info info:支持模糊查询
     * @param name name:支持模糊查询
     * @param page page:页码
     * @param limit limit:每页行数
     * @return ResultEntity
     */
    @GetMapping("getrolelist")
    private ResultEntity getRoleList(
            @RequestParam("info") String info,
            @RequestParam("name") String name,
            @RequestParam("page") int page,
            @RequestParam("limit") int limit
            ){
    ResultEntity resultEntity = new ResultEntity();
    try{
        int start =(page-1)*limit;
        List<RoleEntity> list= roleService.getRoleList(info, name, start, limit);
        if(!info.equals("")||!name.equals("")){
            resultEntity.setCount(list.size());
        }else{
            resultEntity.setCount(roleService.getRoleCount());
        }
        resultEntity.setData(list);
        resultEntity.setMsg("数据获取成功");
        resultEntity.setCode(0);
    }catch (Exception exception){
        exception.printStackTrace();
        resultEntity.setCode(-9999);
        resultEntity.setMsg("数据获取失败！");
    }
    return resultEntity;
    }

    /**
     * 添加角色
     * @param roleVo 角色对象
     * @return ResultEntity
     */
    @PostMapping("role")
    private ResultEntity addRole(@RequestBody RoleVo roleVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(roleService.addRole(roleVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("数据添加成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("数据添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("数据获取失败！");
        }
        return resultEntity;
    }

    /**
     * 更新role
     * @param roleVo roleVo
     * @return ResultEntity
     */
    @PutMapping("role")
    private ResultEntity updateRole(@RequestBody RoleVo roleVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(roleService.updateRole(roleVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("数据修改成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("数据修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("数据修改失败！");
        }
        return resultEntity;
    }

    /**
     * 删除role
     * @param roleid roleid
     * @return ResultEntity
     */
    @DeleteMapping("role/{roleid}")
    private ResultEntity delRole(@PathVariable("roleid") int roleid){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(roleid==13){
                resultEntity.setCode(2);
                resultEntity.setMsg("管理员不允许删除！");
            }else{
                if(roleService.delRole(roleid)>0){
                    resultEntity.setCode(0);
                    resultEntity.setMsg("数据删除成功！");
                }else{
                    resultEntity.setCode(1);
                    resultEntity.setMsg("数据删除失败！");
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("数据删除失败！");
        }
        return resultEntity;
    }

    /**
     * 修改状态
     * @param roleVo roleVo
     * @return ResultEntity
     */
    @PutMapping("changestatus")
    private ResultEntity changeStatus(@RequestBody RoleVo roleVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(roleService.changeStatus(roleVo.getRoleid())>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("状态修改成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("状态修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("状态修改失败！");
        }
        return resultEntity;
    }
}
